package edu.ntu.im.p2ptrade.action;

import javax.servlet.http.HttpServletRequest;

import edu.ntu.im.p2ptrade.P2PTrade;
import edu.ntu.im.p2ptrade.Util;
import edu.ntu.im.p2ptrade.entity.Dialog;
import edu.ntu.im.p2ptrade.entity.DialogRepository;
import edu.ntu.im.p2ptrade.msg.GenericDialog;

public class AddDialog extends AjaxAction {

	@Override
	public AjaxResponse execute(HttpServletRequest request) {
		String peer = request.getParameter("peer");
		String content = request.getParameter("content");
		Dialog dialog = new Dialog();
		dialog.setFrom(P2PTrade.getInstance().getPeerName());
		dialog.setTo(peer);
		dialog.setContent(content);
		DialogRepository.getInstance().addDialog(dialog);
		
		GenericDialog gd = new GenericDialog(P2PTrade.getInstance().getPeerName(),content, peer);
		Util.talkToPeer(gd, peer);
		return success();
	}

}
